import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nft_shop/box/page/details/box_details_page.dart';
import 'package:nft_shop/market/model/models.dart';
import 'package:nft_shop/market/page/details/works_3d_details_page.dart';
import 'package:nft_shop/market/page/details/works_details_page.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/other_utils.dart';
import 'package:nft_shop/widgets/load_image.dart';

class SiftListItemWidget extends StatefulWidget {
  GoodsSpecialList model;
  VoidCallback? onPressed;

  SiftListItemWidget({Key? key, required this.model, this.onPressed})
      : super(key: key);

  @override
  State<SiftListItemWidget> createState() => _SiftListItemWidgetState();
}

class _SiftListItemWidgetState extends State<SiftListItemWidget> {
  TimerUtil? _timerUtil;
  String _countDownTime = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    cancelAddressTimer();
  }

  void cancelAddressTimer() {
    if (_timerUtil != null) {
      _timerUtil!.cancel();
      _timerUtil = null;
    }
  }

  @override
  Widget build(BuildContext context) {
    if (widget.model.isPresell == 1) {
      if (widget.model.second! > 0) {
        cancelAddressTimer();
        _timerUtil = new TimerUtil(mTotalTime: widget.model.second!);
        _timerUtil!.setOnTimerTickCallback((int tick) {
          if (tick == 0) {
            cancelAddressTimer();
            //主页面刷新
            widget.onPressed!();
            return;
          }
          setState(() {
            _countDownTime = Utils.constructTime(tick ~/ 1000);
            widget.model.second = tick;
          });
        });
        // //开始倒计时
        _timerUtil!.startCountDown();
      } else {
        cancelAddressTimer();
        //主页面刷新
        widget.onPressed!();
      }

      // if (DateUtil.getNowDateMs() <  DateUtil.getDateMsByTimeStr(widget.model.sellTime!)!) {
      //   _timerUtil = new TimerUtil(
      //       mTotalTime: DateUtil.getDateMsByTimeStr( widget.model.sellTime!)! - DateUtil.getNowDateMs());
      //   _timerUtil!.setOnTimerTickCallback((int tick) {
      //     if (tick == 0) {
      //       cancelAddressTimer();
      //       //主页面刷新
      //       widget.onPressed!();
      //       return;
      //     }
      //     setState(() {
      //       _countDownTime = Utils.constructTime(tick ~/ 1000);
      //     });
      //   });
      //   // //开始倒计时
      //   _timerUtil!.startCountDown();
      // }
    }

    return siftItemWidget(widget.model);
  }

  ///item
  Widget siftItemWidget(GoodsSpecialList model) {
    double height = Get.width - 30;
    return InkWell(
      onTap: () {
        // 1藏品 2盲盒
        if (model.type == 1) {
          //3D
          if (model.specialTypeId == 2) {
            Get.to(Works3DDetailsPage(
              id: model.id!,
              type: 3,
            ));
          } else {
            Get.to(WorksDetailsPage(
              id: model.id!,
              type: 3,
            ));
          }
        } else {
          Get.to(BoxDetailsPage(id: model.id!, type: 3));
        }
      },
      child: Container(
          margin: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
          decoration: BoxDecoration(
              color: Global.kTheme.colorBackground1,
              borderRadius: BorderRadius.circular(20.0),
              boxShadow: [
                BoxShadow(
                    color: Color(0x80E6E6E6),
                    offset: Offset(2.0, 2.0), //阴影xy轴偏移量
                    blurRadius: 8.0, //阴影模糊程度
                    spreadRadius: 3.0 //阴影扩散程度
                    )
              ]),
          child: Column(
            children: [
              Stack(
                children: [
                  ClipRRect(
                      borderRadius: BorderRadius.circular(20),
                      child: LoadImage(
                        model.thumb ?? "",
                        width: double.infinity,
                        fit: BoxFit.fitHeight,
                        height: (Get.width - 30),
                      )),
                  // widget.index == 3?
                  // Positioned(left: 10, top: 10, child: _objectState(model.num == 0?"已售罄":"首发售卖中")):
                  Positioned(
                      left: 10,
                      top: 10,
                      child: _objectState(model.isPresell == 0
                          ? (model.num == 0 ? "已售罄" : "首发售卖中")
                          // : "敬请期待 ${model.sellTime} 开售")),
                          : "$_countDownTime后 开售")),
                ],
              ),
              Padding(
                padding: EdgeInsets.only(left: 17, right: 17, bottom: 20),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: 13,
                        ),
                        Text(
                          model.title ?? "",
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 16,
                              color: Global.kTheme.textPrimary4,
                              fontFamily: Global.mySemiboldFont),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Row(
                          children: [
                            _objectType(
                              "${model.type == 1 ? "藏品" : "盲盒"}",
                              Global.kTheme.colorBackground3,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            issueView("限量", model.allCount.toString()),
                            SizedBox(
                              width: 10,
                            ),
                            _objectType(
                              "${model.specialTypeName ?? ""}",
                              Global.kTheme.colorBackground6,
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Row(
                          children: [
                            ClipRRect(
                                borderRadius: BorderRadius.circular(9),
                                child: LoadImage(
                                  model.portrait ?? "header_img",
                                  width: 18,
                                  height: 18,
                                  holderImg: "header_img",
                                )),
                            SizedBox(
                              width: 5,
                            ),
                            Text(
                              model.nickname ?? "",
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  fontSize: 12,
                                  color: Global.kTheme.textPrimary0,
                                  fontFamily: Global.mySemiboldFont),
                            )
                          ],
                        )
                      ],
                    ),
                    Spacer(),
                    Text(
                      "¥${model.basePrice ?? ""}",
                      style: TextStyle(
                          fontSize: 24,
                          color: Global.kTheme.textPrimary0,
                          fontFamily: Global.myDINFont),
                    ),
                  ],
                ),
              ),
            ],
          )),
    );
  }

  ///状态
  Widget _objectState(String state) {
    return Container(
      height: 26,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(13),
        color: Global.kTheme.colorBackground3.withAlpha(100),
      ),
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 4),
      child: Text(
        state,
        style: TextStyle(
            color: Global.kTheme.textPrimary5,
            fontSize: 14,
            fontFamily: Global.mySemiboldFont),
      ),
    );
  }

  ///盲盒类型
  Widget _objectType(String state, Color color) {
    return Container(
      height: 20,
      decoration: BoxDecoration(
        color: color,
      ),
      padding: EdgeInsets.symmetric(horizontal: 8, vertical: 2),
      child: Text(
        state,
        style: TextStyle(
            color: Global.kTheme.textPrimary5,
            fontSize: 12,
            fontFamily: Global.mySemiboldFont),
      ),
    );
  }

  Widget issueView(String left, String right) {
    return Row(
      children: [
        Container(
          color: Global.kTheme.colorBackground3,
          padding: EdgeInsets.symmetric(horizontal: 5, vertical: 2),
          child: Text(
            left,
            style: TextStyle(
                fontSize: 12,
                color: Global.kTheme.textPrimary5,
                fontFamily: Global.mySemiboldFont),
          ),
        ),
        Container(
          color: Color(0xFFB6B6B6),
          padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
          child: Text(
            right,
            style: TextStyle(
                fontSize: 12,
                color: Global.kTheme.textPrimary5,
                fontFamily: Global.mySemiboldFont),
          ),
        ),
      ],
    );
  }
}
